import actionTaker from './transaction/actiontaker.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import {LGXEnums}  from '../common/sysenum.js';


class TransactionMngSvc {
    constructor(datamanger) {
        this.dataManger = datamanger;
        this.m_pCareTaker = new actionTaker.ActionTaker(datamanger);
        this.m_tPreStartOpStepGphLst = null;
    }
    record(startFlag, tGraphListIn) {
        console.log("TransactionMngSvc record-begain");
        if (startFlag) {
            let tGraphList = this.getTopoConnGraphs(tGraphListIn);
            this.m_pCareTaker.clearRelate();
            if (tGraphList.length == 0) {
                this.recordCntInfo();
                this.m_pCareTaker.remember(startFlag);
                return 0;
            }
            else {
                for (let i = 0; i < tGraphList.length; i++) {
                    let graph = tGraphList[i];
                    this.m_pCareTaker.addRelateGraph(graph);
                }

                for (let i = 0; i < tGraphList.length; i++) {
                    let graph = tGraphList[i];
                    this.m_pCareTaker.addRelateRes(graph.getTopoRes());
                }
                this.recordCntInfo();
            }
            this.m_tPreStartOpStepGphLst = this.m_pCareTaker.getRelateGraphLst();
        }
        else {
            this.recordNewInfo();
        }

        this.m_pCareTaker.remember(startFlag);
        if (!startFlag) {
            this.eraseNewFlag();
        }
        return 0;
    }


    backward() {
        console.log("backward***************************************");
        let pRect = null;//this.dataManger.getSelRegion();
        if (pRect) {
            this.dataManger.clearMultiSelDatas();
        }

        var tGraphs = new Array();
        this.m_pCareTaker.getChangeGraph(2, tGraphs);

        let failed = this.m_pCareTaker.backward();
        if (failed != 0) {
            return failed;
        }

        let pCurentGraph = this.dataManger.getCurentGraph();
        if (pCurentGraph && pCurentGraph.getUseful() == 0) {
            this.dataManger.setCurentGraph(null);
        }
        return failed;
    }


    forward() {
        var tGraphs = new Array();
        this.m_pCareTaker.getChangeGraph(1, tGraphs);
        let pRect = null;//this.dataManger.getSelRegion();
        if (pRect != null) {
            this.dataManger.clearMultiSelDatas();
        }
        let failed = this.m_pCareTaker.forward();
        return failed;
    }

    recordCntInfo() {
        let pGraph = this.dataManger.getCurentGraph();
        if (!pGraph) {
            return -1;
        }
        let tRelateGph = new Array();
        let tRelateRes = new Array();
        this.dataManger.getRelateTopo(pGraph, tRelateRes, tRelateGph);
        for (let i = 0; i < tRelateGph.length; i++) {
            let graph = tRelateGph[i];
            this.m_pCareTaker.addRelateGraph(graph);
        }

        for (let i = 0; i < tRelateRes.length; i++) {
            let res = tRelateRes[i];
            this.m_pCareTaker.addRelateRes(res);
        }
        return 0;
    }
    recordNewInfo() {
        this.recordNewGraph();
        this.recordNewResource();
        return 0;
    }
    recordNewGraph() {
        let tNewGraphLst = this.dataManger.getCurentNewGraphLst();
        for (let i = 0; i < tNewGraphLst.length; i++) {
            let pGraph = tNewGraphLst.at(i);
            this.m_pCareTaker.addRelateGraph(pGraph);
        }
    }
    recordNewResource() {
        let managePowerRes = this.dataManger.getTopoManager();
        if (!managePowerRes) {
            return -1;
        }

        let nNew = managePowerRes.m_tCurentNewResList.length;
        for (let i = 0; i < nNew; i++) {
            let pRes = managePowerRes.m_tCurentNewResList[i];
            this.m_pCareTaker.addRelateRes(pRes);
        }
        return 0;
    }

    eraseNewFlag() {
        this.eraseNewGraphFlag();
        this.eraseNewResFlag();
        return 0;
    }

    eraseNewResFlag() {
        let managePowerRes = this.dataManger.getTopoManager();
        if (managePowerRes) {
            let nNew = managePowerRes.m_tCurentNewResList.length;
            for (let i = 0; i < nNew; i++) {
                let pRes = managePowerRes.m_tCurentNewResList[i];
                pRes.setNewAdd(2);
            }
            managePowerRes.m_tCurentNewResList.length = 0;
        }
        return 0;
    }

    eraseNewGraphFlag() {
        this.dataManger.m_tThisTimeStoreNewGraphLst.length = 0;
        let nNew = this.dataManger.m_tCurentNewGraphLst.length;
        for (let i = 0; i < nNew; i++) {
            let pGraph = this.dataManger.m_tCurentNewGraphLst[i];
            pGraph.setNewAdd(2);
            this.dataManger.m_tThisTimeStoreNewGraphLst.push(pGraph);
        }
        this.dataManger.m_tCurentNewGraphLst.length = 0;
        return 0;
    }

    removePreStep() {
        this.m_pCareTaker.removePreStep();
    }

    getTopoConnGraphs(graphList) {
        let topoConnGraphArr = [];
        graphList.forEach(p => {
            topoConnGraphArr.push(p);
        });
        for (let i = 0; i < graphList.length; i++) {
            let graph = graphList[i];
            this.getGraphTopoConnOther(graph, topoConnGraphArr);
        }
        return topoConnGraphArr;
    }

    getGraphTopoConnOther(destGraph, topoConnGraphArr) {
        let label = destGraph.getLabel();
        this.addToList(label, topoConnGraphArr);
        let topoRes = destGraph.getTopoRes();
        if (topoRes) {
            let connNodeList = topoRes.getConnNodeList();
            for (let i = 0; i < connNodeList.length; i++) {
                let node = connNodeList[i];
                this.addNodeConnGraph2List(node, destGraph, topoConnGraphArr);
            }
        }
    }

    addNodeConnGraph2List(node, destGraph, topoConnGraphArr) {
        if (destGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
            let refRes = node.getRefRes();
            if (refRes && refRes.getRefGraph()){
                this.addToList(refRes.getRefGraph(), topoConnGraphArr);
            }
        }
        else {
            let connLineList = node.getConnValidLineList();
            for (let i = 0; i < connLineList.length; i++) {
                let connLine = connLineList[i];
                let lineGraph = connLine.getRefGraph();
                if (connLine && lineGraph) {
                    this.addToList(lineGraph, topoConnGraphArr);
                }
            }
        }
    }

    addToList(destGraph, topoConnGraphArr) {
        if (destGraph && topoConnGraphArr.indexOf(destGraph) == -1) {
            topoConnGraphArr.push(destGraph);
        }
    }
}

export default { TransactionMngSvc }